package com.sp.business.normalization;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * a <em><b>non-hibernate-mapped</b></em> placeholder/wrapper class that contains information about
 * a ConfigurationFile that is used during normalization
 */
public class RawFile
{

  private boolean encrypted = false;
  private boolean compressed = false;
  private String filename = null;
  private String name = null;

  public RawFile()
  {
  }

  public RawFile(String name, String filename)
  {
    this.name = name;
    this.filename = filename;
  }

  public boolean isEncrypted()
  {
    return encrypted;
  }
  public void setEncrypted(boolean encrypted)
  {
    this.encrypted = encrypted;
  }
  public boolean isCompressed()
  {
    return compressed;
  }
  public void setCompressed(boolean compressed)
  {
    this.compressed = compressed;
  }
  public String getFilename()
  {
    return filename;
  }
  public void setFilename(String filename)
  {
    this.filename = filename;
  }
  public String getName()
  {
    return name;
  }
  public void setName(String name)
  {
    this.name = name;
  }

  public InputStream getInputStream() throws Exception
  {
    return new BufferedInputStream(new FileInputStream(this.getFilename()));
  }

  public String getFileData() throws Exception
  {
    StringBuilder sb = new StringBuilder();
    int length = 0;
    byte[] buffer = new byte[1024];
    InputStream is = getInputStream();
    while ((length = is.read(buffer)) != -1)
    {
      sb.append(new String(buffer, 0, length));
    }
    is.close();
    return sb.toString();
  }

  public File resolveFile()
  {
    return new File(getFilename());
  }

  @Override
  public String toString()
  {
    return String.format("RawFile [name=%s, filename=%s encrypted=%s, compressed=%s]", name, filename, encrypted, compressed);
  }

}
